package cn.fivestars.springcloud.payment.controller;

import cn.fivestars.springcloud.commons.entities.CommonResult;
import cn.fivestars.springcloud.commons.entities.Payment;
import cn.fivestars.springcloud.payment.serivce.PaymentService;
import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * PaymentController
 *
 * @author liangchenzhou1024@163.com
 * @date 2021/2/1
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/")
    public CommonResult<Payment> save(@RequestBody Payment payment) {
        int result = paymentService.insert(payment);
        if (result > 0) {
            log.info("serverPort:{}, success, payment={}", serverPort, payment);
            return CommonResult.success(payment);
        } else {
            log.error("serverPort:{}, error, payment={}", serverPort, payment);
            return CommonResult.failure(40001, "error", payment);
        }

    }

    @GetMapping("/{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id) {
        Payment payment = paymentService.selectById(id);
        if (payment != null) {
            log.info("serverPort:{}, success, payment={}", serverPort, payment);
            return CommonResult.success(payment);
        } else {
            log.error("serverPort:{}, error, id={}", serverPort, id);
            return CommonResult.failure(40002, "no records");
        }
    }

    @GetMapping("/consul")
    public String consul() {
        return "springCloud with consul:" + serverPort + "\t" + UUID.fastUUID();
    }
}
